<%@ Page Language="C#" MasterPageFile="~/Admin/admin.master" AutoEventWireup="true"
    CodeFile="ReturnGoodEdit.aspx.cs" Inherits="ReturnGoodEdit" Title="Return Good Edit" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
    Grn - Add/Edit</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script src="myscript/ExportAdjustEdit.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
        function checkquantity(obj) {
            var quantity = parseFloat($(obj).val()); //Lay text TextBox
            var stockquantity = parseFloat($("[id$=HdStockQuantity]").val());

            if (quantity > stockquantity) {
                $(obj).val(stockquantity);

            }
           
        }
        
         function HandleKey(combo, e) {
            var key;
            var keychar;

            if (window.event)
                key = window.event.keyCode;
            else if (e)
                key = e.which;
            else
                return true;
//            if (key == 13) {
//                //document.forms[0].submit();
//                document.getElementById('<%= btnSave.ClientID %>').click();
//            }
        }
       
       
        function OnClientSelectedIndexChanged(sender, args) 
        {
            var item = args.get_item();
            GetDrug(item.get_value());

         }
         function SetDefalutButton(e, buttonid) {
            var evt = e ? e : window.event;
            var quantity = parseFloat($("[id$=tbQuantity]").val()); //Lay text TextBox
            var Poquantity = parseFloat($("[id$=HdPoQuantity]").val());
            if (evt.keyCode == 13)/*Search if user hit enter key*/
            {
                if (quantity <= Poquantity) {
                    var bt = $('[id$=' + buttonid + ']');
                    //( bt[0].href);//if it is link button
                    bt.click(); //if it button
                    return false;
                }


            }
        }
         function checkValid()
        {
            var quantity = parseFloat($("[id$=tbQuantity]").val()); //Lay text TextBox
            var stockquantity = parseFloat($("[id$=HdStockQuantity]").val());
            var Errquantity = $("#Sperrquantity"); // Lay MarkupPercent
            if (quantity > stockquantity || quantity<1) {
////                $(obj).val(Poquantity);
               // Errquantity.text('*');
               $("[id$=tbQuantity]").addClass("input-warning");
                return false;
            }
            else 
            {
                //Errquantity.text('');
               
                 return true;
            }
        }
      
    </script>

    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>

    <script src="myscript/ExportAdjustEdit.js" type="text/javascript"></script>

    <data:MultiFormView ID="FormView1" DataKeyNames="StockOutId" runat="server" DataSourceID="StockOutDataSource"
        OnLoad="FormView1_Load" OnItemCommand="FormView1_ItemCommand">
        <EditItemTemplatePaths>
            <data:TemplatePath Path="~/Admin/UserControls/ReturnGoodFields.ascx" />
        </EditItemTemplatePaths>
        <InsertItemTemplatePaths>
            <data:TemplatePath Path="~/Admin/UserControls/ReturnGoodFields.ascx" />
        </InsertItemTemplatePaths>
        <EmptyDataTemplate>
            <b>StockOut not found!</b>
        </EmptyDataTemplate>
        <FooterTemplate>
            <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                CssClass="button" Text="Insert" />
            <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                CssClass="button" Text="Update" />
            <asp:Button ID="PreviewButton" runat="server" CausesValidation="True" Visible="False"
                ToolTip="Print Preview" CssClass="button" Text="Preview" CommandName="Preview" />
            <asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                ToolTip="Return To Manage" CssClass="button" Text="Finish" />
            <asp:Button ID="CloseReturn" runat="server" CausesValidation="False" Visible="false"
                CommandName="CloseReturn" ToolTip="Close Return Goods" CssClass="button" Text="CloseReturnGoods"
                OnClientClick="return confirm('After Close Return Goods cannot change info!');" />
        </FooterTemplate>
    </data:MultiFormView>
    <data:StockOutDataSource ID="StockOutDataSource" runat="server" SelectMethod="GetByStockOutId">
        <Parameters>
            <asp:QueryStringParameter Name="StockOutId" QueryStringField="StockOutId" Type="String" />
        </Parameters>
    </data:StockOutDataSource>
    <br />
    <br />
    <asp:Panel runat="server" ID="pn_ExportAdjustDetailsAdd" Width="100%" Visible="false"
        DefaultButton="btnSave">
        <table>
            <tr>
                <td colspan="8">
                    <h3>
                        Add Details
                    </h3>
                </td>
            </tr>
            <tr>
                <td>
                    Drug Id:
                </td>
                <td>
                    Unit:
                </td>
                <td>
                    Unit Price:
                </td>
                <td>
                    BatchLot:
                </td>
                <td>
                    ExpDate:
                </td>
                <td>
                    Quantity:
                </td>
                <td>
                    Remark:
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td id="radcombo">
                    <telerik:RadComboBox ID="cbo_DrugID" runat="server" Width="200px" CssClass="radcombo"
                        DropDownWidth="610px" EmptyMessage="Choose a Drug" HighlightTemplatedItems="True"
                        EnableLoadOnDemand="True" OnItemsRequested="cbo_DrugID_ItemsRequested" OnClientKeyPressing="HandleKey"
                        TabIndex="10" OnClientSelectedIndexChanged="OnClientSelectedIndexChanged" ValidationGroup="Detail">
                        <HeaderTemplate>
                            <table style="width: 600px" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td style="width: 350px;">
                                        Drug Name
                                    </td>
                                    <td style="width: 50;">
                                        Drug Id
                                    </td>
                                    <td style="width: 100px;">
                                        Batch/Lot
                                    </td>
                                    <td style="width: 50px;">
                                        ExpDate
                                    </td>
                                    <td style="width: 50px;">
                                        Quantity
                                    </td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table style="width: 600px" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td style="width: 350px;">
                                        <%# DataBinder.Eval(Container, "Text")%>
                                    </td>
                                    <td style="width: 50px;">
                                        <%# DataBinder.Eval(Container, "Attributes['DrugId']")%>
                                    </td>
                                    <td style="width: 100px;">
                                        <%# DataBinder.Eval(Container, "Attributes['Batch-Lot']")%>
                                    </td>
                                    <td style="width: 50px;">
                                        <%# DataBinder.Eval(Container, "Attributes['ExpDate']")%>
                                    </td>
                                    <td style="width: 50px;">
                                        <%# DataBinder.Eval(Container, "Attributes['Quantity']")%>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </telerik:RadComboBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic"
                        ControlToValidate="cbo_DrugID" ErrorMessage="Required" ValidationGroup="add"></asp:RequiredFieldValidator>
                </td>
                <td align="left">
                    <span id="UnitResult" style="color: #CC6600; font-weight: bold; min-width: 40px">
                    </span>
                </td>
                <td>
                    <input type="text" runat="server" id="CostPriceResult" class="text-input" tabindex="11"
                        value="" style="width: 100px" enableviewstate="false" readonly="readonly" />
                    <%-- <asp:TextBox ID="CostPriceResult" runat="server" CssClass="text-input" Enabled="false" EnableViewState="false" ></asp:TextBox>--%>
                    <%--<span id="Span1" style="color: #CC6600; font-weight: bold; min-width: 40px">--%>
                </td>
                <%--  <td>
                    <span id="CostPriceResult" style="color: #808000; font-weight: bold"></span>
                </td>--%>
                <td align="left">
                    <input type="text" runat="server" id="TxtBatchLot" class="text-input" tabindex="11"
                        enableviewstate="false" readonly="readonly" value="" style="width: 100px" />
                </td>
                <td align="left">
                    <input type="text" runat="server" id="txtExpDate" class="text-input" tabindex="11"
                        value="" style="width: 120px" enableviewstate="false" readonly="readonly" />
                </td>
                <td>
                    <input type="text" runat="server" id="tbQuantity" class="text-input" tabindex="11"
                        value="1" style="width: 40px" onkeypress="return SetDefalutButton(event,'btnSave');" />
                    <asp:RequiredFieldValidator ID="ReqVal_dataQuantity" runat="server" Display="Dynamic"
                        ControlToValidate="tbQuantity" ErrorMessage="Required" ValidationGroup="add"></asp:RequiredFieldValidator>
                    <asp:HiddenField ID="HdStockQuantity" runat="server" Value="" />
                    <span id="Sperrquantity"></span>
                </td>
                <td>
                    <input type="text" runat="server" id="tbRemark" class="large-input text-input" tabindex="12" />
                </td>
                <td>
                    <asp:Button ID="btnSave" Text="Add" class="button" TabIndex="13" runat="server" OnClick="btnSave_Click"
                        OnClientClick="return checkValid();" ValidationGroup="Detail" />
                    <input type="button" id="btnCancel" value="Cancel" class="button" style="display: none"
                        tabindex="14" />
                </td>
            </tr>
        </table>
    </asp:Panel>
    <br />
    <data:EntityGridView ID="GridViewStockOutDetail" runat="server" AutoGenerateColumns="False"
        DataSourceID="SrockOutDetailsDataSource1" DataKeyNames="StockOutDetailId" AllowMultiColumnSorting="false"
        DefaultSortColumnName="" DefaultSortDirection="Ascending" ExcelExportFileName="Export_ExportAdjustDetails.xls"
        Visible='<%# FormView1.DefaultMode != FormViewMode.Insert %>' OnRowDataBound="GridViewStockOutDetail_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton runat="server" ID="btn_Delete" OnClick="btn_Delete_Click" ValidationGroup="delete">Delete</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="DrugId" HeaderText="DrugId" SortExpression="[DrugId]">
                <ItemStyle HorizontalAlign="Right" />
            </asp:BoundField>
            <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="[Quantity]"
                DataFormatString="{0:#,##0}">
                <ItemStyle HorizontalAlign="Right" />
            </asp:BoundField>
            <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" SortExpression="[UnitPrice]"
                DataFormatString="{0:#,##0.00}">
                <ItemStyle HorizontalAlign="Right" />
            </asp:BoundField>
            <asp:BoundField DataField="ExpDate" HeaderText="Exp Date" SortExpression="[ExpDate]"
                DataFormatString="{0:MMM-yyyy}" />
            <asp:BoundField DataField="BatchLot" HeaderText="Batch Lot" SortExpression="[BatchLot]" />
            <asp:BoundField DataField="Remark" HeaderText="Remark" SortExpression="[Remark]" />
        </Columns>
        <EmptyDataTemplate>
            <b>No Grn Details Found! </b>
        </EmptyDataTemplate>
    </data:EntityGridView>
    <data:StockOutDetailsDataSource ID="SrockOutDetailsDataSource1" runat="server" SelectMethod="Find">
        <Parameters>
            <data:SqlParameter Name="Parameters">
                <Filters>
                    <data:StockOutDetailsFilter Column="StockOutId" QueryStringField="StockOutId" />
                </Filters>
            </data:SqlParameter>
            <data:CustomParameter Name="OrderByClause" Value="" ConvertEmptyStringToNull="false" />
        </Parameters>
    </data:StockOutDetailsDataSource>
</asp:Content>
